<?php 
namespace app\api\controller;
use \think\Controller;
use \think\Db;

class User extends controller{

public function login(){
    $uname=input('username');
    $upassword=input('password');
    $users=Db::name('users')->where('u_name',$uname)->find();
        if ($users)
         {
          if (md5($upassword)==$users['u_password'])
           {
            return json([
                          'error'=>'0',
                          'u_id'=>$users['u_id'],
                          'u_name'=>$users['u_name'],
                          'u_gender'=>$users['u_gender'],
                        
                          'u_pic'=>$users['u_pic'],
                          'u_telephone'=>$users['u_telephone'],
                          'u_address'=>$users['u_address'],
                          'u_num'=>$users['u_num'],
                          'msg'=>'登陆成功',
                        ]);
          }else{
            
            return json([
                          'error'=>'1',
                          'msg'=>'密码错误',
                              ]);
          }
        }else{
          
          return json([
                          'error'=>'2',
                          'msg'=>'用户不存在',
                       ]);
        }


  }

  public function reg(){
        $uname=input('username');
        $uphone=input('userphone');
        $upassword=input('upassword');

        
        $reg=Db::name('users')->where('u_name',$uname)->find();

        if($reg){
              return json([
                          'success'=>'0',
                          'content'=>'用户名已被注册',
                              ]);
        }else if($upassword==null){ 
          return json([
            'success'=>'3',
            'content'=>'密码不呢为空',
                  ]);}
          else if ($uphone==null) {
            return json([
              'success'=>'4',
              'content'=>'电话不能为空',
            ]);
           
          }else{
          $date=[   'u_telephone'=>$uphone,
                    'u_password'=>md5($upassword),
                    'u_name'=>$uname,
                    'u_gender'=>1
                ];
            $users=Db::name('users')->insert($date);
            $user =DB::name('users')->where('u_name',$uname)->find();
          if($users){
                return json([
                          'success'=>'2',
                          'u_id'=>$users['u_id'],
                          'u_name'=>$users['u_name'],
                          'u_gender'=>$users['u_gender'],
                          'u_birthdate'=>$users['u_birthdate'],
                          'u_pic'=>$users['u_pic'],
                          'u_telephone'=>$users['u_telephone'],
                          'u_address'=>$users['u_address'],
                          'u_num'=>$users['u_num'],
                              ]);
          }else{
                return json([
                          'success'=>'1',
                          'content'=>'注册失败',
                              ]);
          }
        }
  }

}